/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.cn.xiaonuo.tcrfinance.tcrbanner.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.constant.Status;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.tcrfinance.tcrbanner.entity.PvBanner;
import com.cn.xiaonuo.tcrfinance.tcrbanner.mapper.PvBannerMapper;
import com.cn.xiaonuo.tcrfinance.tcrbanner.param.BannerParam;
import com.cn.xiaonuo.tcrfinance.tcrbanner.service.PvBannerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PvBannerServiceImpl extends ServiceImpl<PvBannerMapper, PvBanner> implements PvBannerService {

    @Resource
    private PvBannerMapper pvBannerMapper;

    /**
     * 首页查看Banner
     *
     * @param type
     * @return
     */
    @Override
    public BusinessMessage queryBanners(Integer type) {
        BusinessMessage message = new BusinessMessage();
        List<Map<String, Object>> banners = pvBannerMapper.queryBanners(type);
        if (banners.size() > 0) {
            message.setCode("200");
            message.setData(banners);
            message.setSuccess(true);
            message.setMessage("操作成功");
            return message;
        } else {
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("没有查询到banner图");
            return message;
        }
    }

    //后台管理系统banner列表
    @Override
    public BusinessMessage selectBanners(Integer pageSize, Integer pageNum) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        if (null == pageNum || Integer.valueOf(pageNum) <= 1) {
            pageNum = 1;
        }
        if (null == pageSize || Integer.valueOf(pageSize) <= 1) {
            pageSize = 10;
        }
        PageHelper.startPage(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
        List<Map<String, Object>> banners = pvBannerMapper.selectBanners();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(banners);
        if (banners.size() > 0) {
            message.setCode("200");
            message.setData(pageInfo);
            message.setSuccess(true);
            message.setMessage("操作成功");
            return message;
        }
        message.setMessage("操作失败");
        message.setCode("-1");
        message.setSuccess(false);
        return message;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessMessage insertBanner(String title, String imageUrl, String actionSort, String type,String cmdAndroid,String cmdParameter) {
        BusinessMessage message = new BusinessMessage();
        try {
            PvBanner banner = new PvBanner();
            banner.setCmdAndroid(cmdAndroid);
            banner.setCmdParameter(cmdParameter);
            banner.setTitle(title);
            banner.setImageUrl(imageUrl);
            banner.setActionSort(Integer.valueOf(actionSort));
            banner.setType(Integer.valueOf(type));
            banner.setIsDisplay(1);
            banner.setCreatedAt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            pvBannerMapper.insert(banner);
            message.setCode("200");
            message.setSuccess(true);
            message.setMessage("操作成功");
        } catch (Exception e) {
            message.setMessage("操作失败");
            message.setCode("-1");
            message.setSuccess(false);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return message;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessMessage deleteBanner(Integer id) {
        BusinessMessage message = new BusinessMessage();
        try {
            PvBanner banner = pvBannerMapper.selectOne(new QueryWrapper<>(new PvBanner() {{
                setId(id);
            }}));
            if (null != banner) {
                banner.setUpdatedAt(new Date());
                banner.setIsDisplay(0);
                pvBannerMapper.updateById(banner);
                message.setCode("200");
                message.setSuccess(true);
                message.setMessage("操作成功");
            } else {
                message.setMessage("操作失败");
                message.setCode("-1");
                message.setSuccess(false);
            }
        } catch (Exception e) {
            message.setMessage("操作失败");
            message.setCode("-1");
            message.setSuccess(false);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public BusinessMessage updateBanner(BannerParam bannerParam) {
        BusinessMessage message = new BusinessMessage();
        if (bannerParam.getId()==null){
            message.setMessage("bannerId不能为空");
            message.setCode("-1");
            message.setSuccess(false);
            return message;
        }
        try {
            PvBanner banner = pvBannerMapper.selectOne(new QueryWrapper<>(new PvBanner() {{
                setId(bannerParam.getId());
            }}));
            BeanUtil.copyProperties(bannerParam,banner);
            pvBannerMapper.updateById(banner);
            message.setCode("200");
            message.setSuccess(true);
            message.setMessage("操作成功");
        }catch (Exception e){
            message.setMessage("操作失败");
            message.setCode("-1");
            message.setSuccess(false);
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public BusinessMessage queryBannerById(BannerParam bannerParam) {
        BusinessMessage message = new BusinessMessage();
        if (bannerParam.getId()==null){
            message.setMessage("bannerId不能为空");
            message.setCode("-1");
            message.setSuccess(false);
            return message;
        }
        PvBanner banner = pvBannerMapper.selectOne(new QueryWrapper<>(new PvBanner() {{
            setId(bannerParam.getId());
        }}));
        message.setMessage(Status.success);
        message.setCode(Status.successCode);
        message.setSuccess(true);
        message.setData(banner);
        return message;
    }
}
